import api from "../../../common/api";
var app = getApp();
Page({
  data: {
    added: '待补充',
    showFillBut: true, //是否显示填写按钮
    chooseFillList: '', //选择了某一条数据
    showTextarea: false, //展示输入框
    height: 0,
    top: 0,
    textInfo5X: '',
    textInfo: '', //输入框的数据
    valueLength: '0', //输入框输入的长度
    isShowCertification: false, //展示挽留组件
    authenticationInfo: {}, //认证信息
    listBut: [], //宣言类型

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
    });
    getApp().globalData.loginPromise.then(() => {
      this.getNavHeight(); //自定义导航栏高度
      // this.authenticationInfo() //获取认证信息
      // this.authType() //宣言类型
      this.getInfoList()
      if (options && options.cooperation) {
        this.fillInfo()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    let userId = getApp().globalData.sessionUser.id
    let result = {
      title: '可信赖的房地产经纪人合作平台',
      path: `/pages/cooperation/editCertification/editCertification?type=share&userId=${userId}&agentId=${userId}`,
      imageUrl: 'https://shulan-prod.oss-cn-shanghai.aliyuncs.com/static/jjrjs/resource/recommend_join.png',
    }
    return result
  },

  // ==========2021-07-27数据整合     页面数据整合=========
  getInfoList() {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中...',
      })
      api.getAuthenticationInfo({}, (res) => {
        if (res && res.data && res.data.content) {
          let content = res.data.content.result //展示的数据
          let tmpType = res.data.content.reportType //宣言类型
          let formatList = []
          for (let i in tmpType) {
            let tmp = {}
            tmp.key = i
            tmp.value = tmpType[i]
            formatList.push(tmp)
          }
          this.setData({
            authenticationInfo: content,
            added: content.declaration !== '' ? content.declaration : '待补充',
            listBut: [...formatList]
          })
          wx.hideLoading()
          resolve()
        }
      })
    })

  },
  // 填写/修改
  fillInfo() {
    let that = this
    that.setData({
      added: '猜你想说',
      showFillBut: false
    })
  },
  // 动态获取输入的值
  dynamicValue(e) {
    let value = e.detail.value
    this.setData({
      valueLength: value.length,
      textInfo: value
    })
  },

  // 选择列表
  fillInfoBut(e) {
    let chooseList = e.currentTarget.dataset.key
    let value = e.currentTarget.dataset.value
    this.setData({
      chooseFillList: chooseList,
      showTextarea: chooseList == '5' ? true : false,
      textInfo5X: value
    })
  },

  // 设置导航栏的高度
  getNavHeight() {
    this.setData({
      height: app.globalData.navHeight,
      top: app.globalData.navTop,
    })
  },
  // 点击返回按钮
  goToBack() {
    let {
      authenticationInfo
    } = this.data
    if (authenticationInfo.declaration !== '') {
      wx.redirectTo({
        url: '/pages/index/index?tabConentIndex=3',
      })
    } else {
      this.setData({
        isShowCertification: true
      })
    }
  },

  // 点击确定按钮
  confirm() {
    let {
      chooseFillList,
      textInfo,
      textInfo5X
    } = this.data
    if (chooseFillList && chooseFillList !== '5') {
      this.authenticationSave(textInfo5X)
    } else if (textInfo) {
      // 全部输入数字的正则
      let num = /[0-9]/g.test(textInfo)
      // 是否输入了表情
      let meg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig.test(textInfo)
      // 是否全部是特俗符号
      var regEn = /^[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]*$/,
        regCn = /^[·！#￥（——）：；“”‘、，|《。》？、【】[\]]*$/;
      let symbol = regEn.test(textInfo),
        symbolX = regCn.test(textInfo)
      // 不允许输入字母
      let letter = /[A-Za-z]/g.test(textInfo)
      if (num) {
        wx.showToast({
          title: '你输入了数字，只能输入中文和字符',
          icon: 'none',
          duration: 1000
        })
      } else if (meg) {
        wx.showToast({
          title: '你输入了表情，只能输入中文和字符',
          icon: 'none',
          duration: 1000
        })
      } else if (symbol || symbolX) {
        wx.showToast({
          title: '你全部输入了符号，只能输入中文和字符',
          icon: 'none',
          duration: 1000
        })
      } else if (letter) {
        wx.showToast({
          title: '你输入了字母，只能输入中文和字符',
          icon: 'none',
          duration: 1000
        })
      } else {
        this.authenticationSave(textInfo)
      }
    }

  },

  // 保存/修改宣言
  authenticationSave(declaration) {
    return new Promise((resolve, reject) => {
      api.authenticationSave({
        declaration
      }, (res) => {
        if (res && res.data && res.data.isSuccess) {
          this.getInfoList()
          this.setData({
            showFillBut: true,
            chooseFillList: '',
            textInfo: '',
            showTextarea: false
          })
        } else {
          wx.showToast({
            title: res.data.errorMessage,
            icon: 'none',
            duration: 1000
          })
        }
      })
    })
  },
  // 以后再说
  closeButX() {
    this.setData({
      isShowCertification: false
    })
    wx.redirectTo({
      url: '/pages/index/index?tabConentIndex=3',
    })
  },
  // 立即补充or填写
  immediatelyAddedX() {
    this.setData({
      isShowCertification: false
    })
    this.fillInfo()
  },
})